package com.sctvcloud.bazhong.ui.adapter.holder;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.gridsum.tracker.GridsumWebDissector;
import com.iflytek.cloud.thirdparty.V;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.application.Cache;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.beans.NewsItem;
import com.sctvcloud.bazhong.ui.utils.GlideUtil;
import com.sctvcloud.bazhong.ui.utils.SkipUtil;
import com.sctvcloud.bazhong.ui.utils.UIUtils;
import com.sctvcloud.bazhong.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 24小时新闻条目
 * Created by Administrator on 2017/7/5.
 */

public class News24Holder extends BaseAbsHolder<NewsItem> implements View.OnClickListener {
    @BindView(R.id.normal_layout)
    protected ConstraintLayout normalLayout;
    @BindView(R.id.item_news_dot)
    protected View dot;
    @BindView(R.id.item_news_line_top)
    protected View topLine;
    @BindView(R.id.item_news_line_bottom)
    protected View bottomLine;
    @BindView(R.id.item_news_divider)
    protected View divider;
    @BindView(R.id.item_news_image)
    protected CustomEXImageView img;
    @BindView(R.id.item_news_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_news_views)
    protected CustomFontTextView readviews;
    @BindView(R.id.item_news_24_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_news_date)
    protected CustomFontTextView date;
    @BindView(R.id.item_news_mode_tag)
    protected CustomFontTextView liveTag;
    @BindView(R.id.item_news_tag)
    protected CustomFontTextView tag;
    @BindView(R.id.item_news_24_length)
    protected CustomFontTextView length;
    @BindView(R.id.item_news_24_media)
    protected ImageView media;

    @BindView(R.id.item_big_news)
    protected LinearLayout itemBigLayout;
    @BindView(R.id.item_news_big_dot)
    protected View bigDot;
    @BindView(R.id.item_news_line_big_top)
    protected View bigTopLine;
    @BindView(R.id.item_news_line_big_bottom)
    protected View bigBottomLine;
    @BindView(R.id.item_news_big_image)
    protected CustomEXImageView bigImg;
    @BindView(R.id.item_news_big_title)
    protected CustomFontTextView bigNewsTitle;
    @BindView(R.id.item_news_big_date)
    protected CustomFontTextView bigNewsDate;
    @BindView(R.id.item_news_big_views)
    protected CustomFontTextView bigNewsViews;
    @BindView(R.id.item_news_24_big_media)
    protected ImageView bigMedia;
    @BindView(R.id.item_news_mode_big_tag)
    protected CustomFontTextView liveBigTag;
    @BindView(R.id.item_news_24_big_length)
    protected CustomFontTextView bigLength;
    @BindView(R.id.item_news_big_divider)
    protected View bigDivider;
    @BindView(R.id.li_nopic)
    protected LinearLayout li_nopic;
    private NewsItem temp;
    @BindView(R.id.item_news_divider_nopic)
    protected View item_news_divider_nopic;

    @BindView(R.id.item_news_title_nopic)
    protected CustomFontTextView item_news_title_nopic;
    @BindView(R.id.item_news_date_nopic)
    protected CustomFontTextView item_news_date_nopic;
    @BindView(R.id.item_news_views_nopic)
    protected CustomFontTextView item_news_views_nopic;

    public News24Holder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(NewsItem t) {
        temp = t;

        if (TextUtils.isEmpty(t.getDataImg())) {
            normalLayout.setVisibility(View.GONE);
            itemBigLayout.setVisibility(View.GONE);
            li_nopic.setVisibility(View.VISIBLE);

            item_news_title_nopic.setText(t.getDataTitle());
            item_news_date_nopic.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
            item_news_views_nopic.setVisibility(View.VISIBLE);
            item_news_views_nopic.setText(UIUtils.getReadingNumber(t.getReadCount()));

            if (t.getAfterTitleMode() > 0) {
                if (t.getAfterTitleMode() == 1) {
                    ViewUtils.setViewsGone(item_news_divider_nopic);
                } else if (t.getAfterTitleMode() == 2) {
                    ViewUtils.setViewsGone(item_news_divider_nopic);
                }
            } else {
                ViewUtils.setViewsVisible(item_news_divider_nopic);
            }

        } else if (temp.getIsBigPic() == 1) {
            //大图显示
            normalLayout.setVisibility(View.GONE);
            li_nopic.setVisibility(View.GONE);
            itemBigLayout.setVisibility(View.VISIBLE);
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(bigImg);

            bigNewsTitle.setText(t.getDataTitle());
            bigNewsDate.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
            bigNewsViews.setText(UIUtils.getReadingNumber(t.getReadCount()));


            if (t.isLiving()) {
                ViewUtils.setViewsGone(bigLength, bigMedia);
                liveBigTag.setVisibility(View.VISIBLE);
            } else {
                liveBigTag.setVisibility(View.GONE);
                if (t.getDataType() > 1) {
                    ViewUtils.setViewVisible(bigMedia);
                    if (TextUtils.isEmpty(t.getDataMediaLength())) {
                        bigLength.setVisibility(View.GONE);
                    } else {
                        String len = TimeUtils.formatTimeToHM(t.getDataMediaLength());
//                        if (!TextUtils.isEmpty(len)) {
//                            bigLength.setVisibility(View.VISIBLE);
//                            bigLength.setText(len);
//                        } else {
//                            bigLength.setVisibility(View.GONE);
//                        }
                        bigLength.setVisibility(View.GONE);
                    }
                } else {
                    ViewUtils.setViewsGone(bigLength, bigMedia);
                }
            }
            if (t.getAfterTitleMode() > 0) {
                if (t.getAfterTitleMode() == 1) {
                    ViewUtils.setViewsGone(bigDivider);
                    bigTopLine.setVisibility(View.INVISIBLE);
                } else if (t.getAfterTitleMode() == 2) {
                    ViewUtils.setViewsGone(bigDivider);
                }
            } else {
                ViewUtils.setViewsVisible(bigDot, bigDivider, bigTopLine);
            }
            if (t.getNewsType() == 2) { //音频
                bigMedia.setImageResource(R.mipmap.icon_voice);
                bigMedia.setVisibility(View.VISIBLE);
            } else if (t.getNewsType() == 3) {   //视频
                bigMedia.setImageResource(R.mipmap.icon_play);
                bigMedia.setVisibility(View.VISIBLE);
            } else {
                bigMedia.setVisibility(View.GONE);
            }
        } else {
            normalLayout.setVisibility(View.VISIBLE);
            itemBigLayout.setVisibility(View.GONE);
            li_nopic.setVisibility(View.GONE);
            if (t.getItemMode() > 0) {
                dot.setVisibility(View.GONE);
                if (t.getItemMode() == 1) {
                    topLine.setVisibility(View.GONE);
                } else {
                    topLine.setVisibility(View.VISIBLE);
                }
                divider.setVisibility(View.GONE);
            } else {
                if (dot.getVisibility() != View.VISIBLE) {
                    dot.setVisibility(View.VISIBLE);
                    topLine.setVisibility(View.VISIBLE);
                    divider.setVisibility(View.VISIBLE);
                }
            }
            if (t.isLiving()) {
                ViewUtils.setViewsGone(length, media);
                liveTag.setVisibility(View.VISIBLE);
            } else {
                liveTag.setVisibility(View.GONE);
                if (t.getDataType() > 1) {
                    ViewUtils.setViewVisible(media);
//                    if (TextUtils.isEmpty(t.getDataMediaLength())) {
//                        length.setVisibility(View.GONE);
//                    } else {
//                        length.setVisibility(View.VISIBLE);
//                        length.setText(TimeUtils.formatTimeToHM(t.getDataMediaLength()));
//                    }
                    length.setVisibility(View.GONE);
                } else {
                    ViewUtils.setViewsGone(length, media);
                }
            }
            if (t.getAfterTitleMode() > 0) {
                if (t.getAfterTitleMode() == 1) {
                    ViewUtils.setViewsGone(dot, divider, topLine);
                } else if (t.getAfterTitleMode() == 2) {
                    ViewUtils.setViewsGone(dot, divider);
                }
            } else {
                ViewUtils.setViewsVisible(dot, divider, topLine);
            }
            String timeStr = DateUtils.getDateHourString(DateUtils.getDataDate(t));
            time.setText(timeStr);
            date.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
            //t.setReadCount(1000000);
            readviews.setVisibility(View.VISIBLE);
            readviews.setText(UIUtils.getReadingNumber(t.getReadCount()));
            title.setText(t.getDataTitle());
            if (!TextUtils.isEmpty(t.getDataTag())) {
                tag.setText(t.getDataTag());
            }
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(img);
            if (t.getNewsType() == 2) { //音频
                media.setImageResource(R.mipmap.icon_home_voice);
                media.setVisibility(View.VISIBLE);
            } else if (t.getNewsType() == 3) {   //视频
                media.setImageResource(R.mipmap.icon_home_play);
                media.setVisibility(View.VISIBLE);
            } else {
                media.setVisibility(View.GONE);
            }
        }

        //区县，隐藏时间线
        topLine.setVisibility(View.GONE);
        dot.setVisibility(View.GONE);
        bottomLine.setVisibility(View.GONE);
        bigTopLine.setVisibility(View.GONE);
        bigBottomLine.setVisibility(View.GONE);
        bigDot.setVisibility(View.GONE);
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
        if (mode == ITEMMODE_LAST) {
            bottomLine.setVisibility(View.GONE);
        } else {
//            bottomLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getNewsTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getNewsTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        SkipUtil.skipWithNewsItem(context, temp, map);
    }
}
